#ifdef a
#include"Sqlist.h"

void TestSqList() {
	SqList sqlist;
	printf("\n插入测试\n");
	Sqlist_Init(&sqlist, 2);
	SqList_In_Back(&sqlist, 0);
	SqList_In_Back(&sqlist, 1);
	SqList_In_Back(&sqlist, 2);
	SqList_Print(&sqlist);
	SqList_In_Head(&sqlist, 0);
	SqList_In_Head(&sqlist, 1);
	SqList_In_Head(&sqlist, 2);
	SqList_Print(&sqlist);
	// 2 1 0 0 1 2
	SqList_In_Pos(&sqlist, 0, 3);
	//3 2 1 0 0 1 2
	SqList_In_Pos(&sqlist, 7, 3);
	//3 2 1 0 0 1 2 3
	SqList_In_Pos(&sqlist, 4, 5);
	//3 2 1 0 5 0 1 2 3
	SqList_Print(&sqlist);
	
	printf("size = %d\n", SqList_Size(&sqlist));
	printf("capacity = %d\n", SqList_Capacity(&sqlist));

	//以上为插入测试：
	//3 2 1 0 5 0 1 2 3
//--------------------------------------------
// 以下为查找测试
	int x;
	printf("请输入要查找的数>>");
	scanf("%d", &x);
	int ret = SqList_Search(&sqlist,x);
	if (ret != -1)
		printf("%d下标%d", x, ret);
	else
		printf("该数不存在\n");


	//3 2 1 0 5 0 1 2 3
//--------------------------------------------------
	//以下为删除测试
	printf("\n删除测试\n");

	SqList_Del_Head(&sqlist);
	SqList_Del_Head(&sqlist);
	SqList_Print(&sqlist);
	//1 0 5 0 1 2 3
	
	SqList_Del_Back(&sqlist);
	SqList_Del_Back(&sqlist);
	SqList_Print(&sqlist);
	//1 0 5 0 1

	SqList_Del_Pos(&sqlist, 0);
	SqList_Del_Pos(&sqlist, 3);
	SqList_Del_Pos(&sqlist, 1);
	SqList_Print(&sqlist);
	//0 0
	printf("size = %d\n", SqList_Size(&sqlist));
	printf("capacity = %d\n", SqList_Capacity(&sqlist));

	//-------
	//销毁
	SqList_Del(&sqlist);
	printf("size = %d\n", SqList_Size(&sqlist));
	printf("capacity = %d\n", SqList_Capacity(&sqlist));


}

//-------------------------------------------------------------------------------

//初始化
void Sqlist_Init(SqList* ps, int put_capacity) {
	assert(ps);
	put_capacity = put_capacity > 0 ? put_capacity : 5;//当初始空间赋值有误时，默认初始空间为5
	ps->array = (TYPE*)malloc(sizeof(TYPE) * put_capacity);
	if (!ps->array) {
		assert(0);
		return;
	}
	ps->size = 0;
	ps->capacity = put_capacity;
	return;
}

//检查容量
void Check_capacity(SqList* ps) {
	assert(ps);
	if (ps->capacity == ps->size) {
		TYPE* temp = realloc(ps->array, sizeof(TYPE) * 2 * ps->capacity);
		if (!temp) {
			assert(0);
			return;
		}
		ps->array = temp;
		ps->capacity *= 2;
		printf("扩容成功！\n");
		return;
	}
	return;
}

//查空
int Check_NULL(SqList* ps) {
	assert(ps);
	return ps->size == 0;
}
//尾插
void SqList_In_Back(SqList* ps, int data) {
	Check_capacity(ps);//内含assert
	ps->array[ps->size++] = data;
	return;
}


//尾删
extern void SqList_Del_Back(SqList* ps) {
	if (Check_NULL(ps))//内含assert
		return;
	ps->size--;
	return;
}

//头插
void SqList_In_Head(SqList* ps, int data) {
	Check_capacity(ps);
	for (int i = ps->size; i >= 0; i--)
		ps->array[i + 1] = ps->array[i];
	ps->array[0] = data;
	ps->size++;
	return;
}

//头删
void SqList_Del_Head(SqList* ps) {
	if (Check_NULL(ps))
		return;
	for (size_t i = 0; i < ps->size - 1; i++) {
		ps->array[i] = ps->array[i + 1];
	}
	ps->size--;
	return;
}

//打印
void SqList_Print(SqList* ps) {
	assert(ps);
	for (size_t i = 0; i < ps->size; i++)
		printf("%d ", ps->array[i]);
	printf("\n");
	return;
}

//有效节点数量
size_t SqList_Size(SqList* ps) {
	assert(ps);
	return ps->size;
}

//容量
size_t SqList_Capacity(SqList* ps) {
	assert(ps);
	return ps->capacity;
}

//任意位置下标插入
void SqList_In_Pos(SqList* ps, int pos, int data) {
	Check_capacity(ps);
	for (int i = ps->size; i >= pos; i--) 
		ps->array[i + 1] = ps->array[i];
	ps->array[pos] = data;
	ps->size++;
	return;
}

//任意位置删除
void SqList_Del_Pos(SqList* ps, int pos) {
	if (Check_NULL(ps)) 
		return;
	for (size_t i = pos; i < ps->size - 1; i++) {
		ps->array[i] = ps->array[i + 1];
	}
	ps->size--;
	return;
}

//顺序表查找(成功返回下标，失败返回-1)
int SqList_Search(SqList* ps, int data) {
	assert(ps);
	for (size_t i = 0; i < ps->size; i++) {
		if (ps->array[i] == data) {
			return i;
		}
	}
	return -1;
}

//顺序表销毁
void SqList_Del(SqList* ps) {
	assert(ps);
	if (ps->array) {
		free(ps->array);
		ps->array = NULL;
		ps->size = 0;
		ps->capacity = 0;
	}
	printf("销毁成功\n");
	return;
}
#endif 